﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Biblioteca.classesBasicas.jogo;
using Biblioteca.fachada;

namespace Desktop.views.cruds.crudsJogo.crudReserva
{
    public partial class CadastroReserva : Form
    {
        private MenuReserva menuReserva;
        private Reserva reserva = new Reserva();
        private Fachada fachada = new Fachada();
        private Campo campo = new Campo();
        private List<Campo> listaCampos;
        private bool campoSelecionado;

        
        public CadastroReserva(MenuReserva menuReserva)
        {
            this.menuReserva = menuReserva;

            menuReserva.Hide();
            InitializeComponent();
        }

        private void buttonCadastrar_Click(object sender, EventArgs e)
        {
            try{
            if (campoSelecionado == false)
            {
                MessageBox.Show("O Campo não foi selecionado para a Reserva.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                reserva.Data = maskedTextBoxData.Text.ToString();
                reserva.Horario = maskedTextBoxHorario.Text.ToString();
                reserva.Duracao = textBoxDuracao.Text.ToString();
                reserva.QuantidadeJogadores = textBoxNdeJogadores.Text.ToString();
                reserva.Campo = campo;

                fachada.cadastrarReserva(reserva);

                menuReserva.carregarGridViewReserva();
                menuReserva.Show();
                this.Dispose();
            }
            MessageBox.Show("Cdastrado com sucesso");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonCancelar_Click(object sender, EventArgs e)
        {
            menuReserva.Show();
            this.Dispose();
        }

        private void CadastroReserva_FormClosed(object sender, FormClosedEventArgs e)
        {
            menuReserva.Show();
            this.Dispose();
        }

        private void CadastroReserva_Load(object sender, EventArgs e)
        {
            carregarGridViewReservaCampo();
        }

        private void dataGridViewReservaCampo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            
            carregarGridViewReservaCampo();

            if (e.RowIndex > -1)
            {
                DataGridViewRow row = this.dataGridViewReservaCampo.Rows[e.RowIndex];

                
                if (DialogResult.Yes == MessageBox.Show("Tem certeza que deseja selecionar este Campo?", "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    campo.Id = row.Cells[0].Value.ToString();
                    campo.Tipo = row.Cells[1].Value.ToString();
                    campo.Tamanho = row.Cells[2].Value.ToString();

                    row.DefaultCellStyle.BackColor = Color.LightBlue;
                    
                    campoSelecionado = true;
                }
                else 
                {
                    row.DefaultCellStyle.BackColor = Color.White;

                    campoSelecionado = false;
                }
            }               
        }


        public void carregarGridViewReservaCampo()
        {
            listaCampos = fachada.listarCampo();

            DataGridViewCellStyle columnHeaderStyle =
            new DataGridViewCellStyle();
            columnHeaderStyle.BackColor = Color.Aqua;
            columnHeaderStyle.Font =
                new Font("Verdana", 10, FontStyle.Bold);
            this.dataGridViewReservaCampo.ColumnHeadersDefaultCellStyle =
                columnHeaderStyle;

            dataGridViewReservaCampo.DataSource = listaCampos;

            this.dataGridViewReservaCampo.Columns[0].HeaderText = "ID";
            this.dataGridViewReservaCampo.Columns[1].HeaderText = "TIPO";
            this.dataGridViewReservaCampo.Columns[2].HeaderText = "TAMANHO";
        }


        private void maskedTextBoxHorario_MouseClick(object sender, MouseEventArgs e)
        {
            this.maskedTextBoxHorario.SelectionStart = 0;
        }

        private void maskedTextBoxData_MouseClick(object sender, MouseEventArgs e)
        {
            this.maskedTextBoxData.SelectionStart = 0;
        }

        //Validação individual
        public void validacaoTextBox()
        {
            //if (textBoxNome.Text == "")
            //{
            //    textBoxNome.Focus();

            //    MessageBox.Show("Por favor, informar o nome do cliente:", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            //}
            //else if (maskedTextBoxRg.Text =="") 
            //{
            //    maskedTextBoxRg.Focus();

            //    MessageBox.Show("Tem certeza que quer cadastrar cliente sem Rg?", "Aviso", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            //    //if MessageBoxButtons.YesNo == true 

            //}            
        }



        //Metódo cor entrada TextBox
        private void CorEntrada(TextBox tb)
        {
            tb.BackColor = Color.LightBlue;
        }
        //Metódo cor saída TextBox
        private void CorSaida(TextBox tb)
        {
            tb.BackColor = Color.White;
        }


        //Metódo cor entrada MaskedTextBox
        private void CorEntrada(MaskedTextBox tb)
        {
            tb.BackColor = Color.LightBlue;
        }
        //Metódo cor saída MaskedTextBox
        private void CorSaida(MaskedTextBox tb)
        {
            tb.BackColor = Color.White;
        }


        //Evento TextBox Enter
        private void textBoxDuracao_Enter(object sender, EventArgs e)
        {
            CorEntrada((TextBox)sender);
        }
        //Evento TextBox Leave
        private void textBoxDuracao_Leave(object sender, EventArgs e)
        {
            CorSaida((TextBox)sender);
        }


        //Evento MaskedTextBox Enter
        private void maskedTextBoxHorario_Enter(object sender, EventArgs e)
        {
            CorEntrada((MaskedTextBox)sender);
        }
        //Evento MaskedTextBox Leave
        private void maskedTextBoxHorario_Leave(object sender, EventArgs e)
        {
            CorSaida((MaskedTextBox)sender);
        }            
         
    }
}
